<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<script src="http://vkontakte.ru/js/api/openapi.js" type="text/javascript"></script>

<div id="vk_api_transport"></div> 

<!-- call example
<div id="vk_login_button" onclick="doLogin();"></div>
-->


<script language="javascript">  
  window.vkAsyncInit = function() {
    VK.init({
       apiId: 2234185
    });
    VK.UI.button('vk_login_button');
    VK.Observer.subscribe('auth.login', function(response) {
       //alert('login');
    });  
    VK.Observer.subscribe('auth.logout', function(response) {
        //alert('logout');
        //myLogout();
    });  
  };
  setTimeout(function() {
    var el = document.createElement('script');
    el.type = 'text/javascript';
    el.src = 'http://vkontakte.ru/js/api/openapi.js';
    el.charset="windows-1251";
    el.async = true;
    document.getElementById('vk_api_transport').appendChild(el);
  }, 0);
  

function doLogin() {
    VK.Auth.login(
      myLogin,
      VK.access.FRIENDS | VK.access.WIKI 
    );
}
function doLogout() {
     //myLogout();
     //VK.Auth.revokeGrants(alert('VK.Auth.revokeGrants'));
     VK.Auth.logout();
}
function myLogin() {
    //alert('call myLogin()');
    window.top.location.href = '<c:out value="${redirectUrl}"/>';
    /*if (!window.timesloaded) {
    window.timesloaded = 1;
    <c:if test="${not empty onFirstTimeLoadVK}">
      <c:out value="${onFirstTimeLoadVK}"/>
    </c:if>
    } else {
      window.timesloaded++;
    }
    if (window.timesloaded > 1) {
      window.top.location.href = '<c:out value="${redirectUrl}"/>';
    }*/
}
function myLogout() {
   alert(document.cookie);  
   var vkCookieName = "vk_app_2234185";
   removeCookie(vkCookieName);
   //var vkCook = getCookie(vkCookieName);
   //if(vkCook){
    //alert('vkCook = ' + vkCook);
   //}
   //addCookie("vk_app_239",null,1);
   //removeCookie("vk_app_239"); 
   alert(document.cookie);  
}
/*
                    function getCookie(name) {
                        var pattern = "(?:; )?" + name + "=([^;]*);?";
                        var regexp  = new RegExp(pattern);
                        
                        if (regexp.test(document.cookie))
                        return decodeURIComponent(RegExp["$1"]);
                        
                        return false;
                    }
                    function addCookie(szName,szValue,dtDaysExpires) 
                    {
                       var dtExpires = new Date();
                       var dtExpiryDate = "";
                    
                       dtExpires.setTime(dtExpires.getTime() + 
                         dtDaysExpires * 24 * 60 * 60 * 1000);
                    
                       dtExpiryDate = dtExpires.toGMTString();
                    
                       document.cookie = 
                       szName + "=" + szValue + "; expires=" + dtExpiryDate;
                    }
                    function removeCookie(szName) 
                    {
                      //alert('removing cookie = '+ szName);
                      var dtExpires = new Date();
                      dtExpires.setTime(dtExpires.getTime() - 1);
                    
                      var szValue = findCookie(szName);
                      //alert('finded cookie = '+ szName + ' value = ' + szValue);
                        
                      document.cookie = szName + "=" + szValue + "; ";
                      //+ "; expires=" + dtExpires.toGMTString();
                    
                      
                      //document.cookie = szName + "=" + szValue + "; ";
                      // + "expires=" + dtExpires.toGMTString();
                    }
                    function findCookie(szName) 
                    {
                      var i = 0;
                      var nStartPosition = 0;
                      var nEndPosition = 0;  
                      var szCookieString = document.cookie;  
                    
                      while(i <= szCookieString.length) 
                      {
                        nStartPosition = i;
                        nEndPosition = nStartPosition + szName.length;
                    
                        if(szCookieString.substring( 
                            nStartPosition,nEndPosition) == szName) 
                        {
                          nStartPosition = nEndPosition + 1;
                          nEndPosition = 
                            document.cookie.indexOf(";",nStartPosition);
                    
                          if(nEndPosition < nStartPosition)
                            nEndPosition = document.cookie.length;
                          return document.cookie.substring( 
                              nStartPosition,nEndPosition);  
                          break;    
                        }
                        i++;  
                      }
                      return "";
                    }
*/
 
</script>